package com.example.e03;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;

public class SQLiteActivity extends AppCompatActivity {

    private DBConnection dbHelper;
    private SQLiteDatabase db;
    private Cursor cursor;
    private EditText nameEdit;
    private EditText phoneEdit;
    private EditText addressEdit;
    private EditText emailEdit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sqlite);

        dbHelper = new DBConnection(this, "Contacts.db", null, 1);
        nameEdit = findViewById(R.id.name);
        phoneEdit = findViewById(R.id.phone);
        addressEdit = findViewById(R.id.address);
        emailEdit = findViewById(R.id.email);

        Button createDbButton = findViewById(R.id.create_db);
        Button openDbButton = findViewById(R.id.open_db);
        Button prevRecordButton = findViewById(R.id.prev_record);
        Button nextRecordButton = findViewById(R.id.next_record);
        Button addButton = findViewById(R.id.add);
        Button modifyButton = findViewById(R.id.modify);
        Button deleteButton = findViewById(R.id.delete);
        Button closeButton = findViewById(R.id.close);

        createDbButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                db = dbHelper.getWritableDatabase();
                Toast.makeText(SQLiteActivity.this, "数据库创建成功", Toast.LENGTH_SHORT).show();
            }
        });

        openDbButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (db == null) {
                    db = dbHelper.getWritableDatabase();
                }
                cursor = db.query("Contacts", null, null, null, null, null, null);
                if (cursor.moveToFirst()) {
                    showRecord(cursor);
                }
                Toast.makeText(SQLiteActivity.this, "数据库打开成功", Toast.LENGTH_SHORT).show();
            }
        });

        prevRecordButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (cursor != null && cursor.moveToPrevious()) {
                    showRecord(cursor);
                }
            }
        });

        nextRecordButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (cursor != null && cursor.moveToNext()) {
                    showRecord(cursor);
                }
            }
        });

        addButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (db != null) {
                    String name = nameEdit.getText().toString();
                    String phone = phoneEdit.getText().toString();
                    String address = addressEdit.getText().toString();
                    String email = emailEdit.getText().toString();
                    db.execSQL("INSERT INTO Contacts (name, phone, address, email) VALUES (?,?,?,?)",
                            new String[]{name, phone, address, email});
                    Toast.makeText(SQLiteActivity.this, "添加成功", Toast.LENGTH_SHORT).show();
                }
            }
        });

        modifyButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (cursor != null && db != null) {
                    int idIndex = cursor.getColumnIndex("id");
                    if (idIndex == -1) {
                        Toast.makeText(SQLiteActivity.this, "数据列'id'不存在", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    int id = cursor.getInt(idIndex);
                    String name = nameEdit.getText().toString();
                    String phone = phoneEdit.getText().toString();
                    String address = addressEdit.getText().toString();
                    String email = emailEdit.getText().toString();
                    db.execSQL("UPDATE Contacts SET name = ?, phone = ?, address = ?, email = ? WHERE id = ?",
                            new String[]{name, phone, address, email, String.valueOf(id)});
                    Toast.makeText(SQLiteActivity.this, "修改成功", Toast.LENGTH_SHORT).show();
                }
            }
        });


        deleteButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (cursor != null && db != null) {
                    int idIndex = cursor.getColumnIndex("id");
                    if (idIndex == -1) {
                        Toast.makeText(SQLiteActivity.this, "数据列'id'不存在", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    int id = cursor.getInt(idIndex);
                    db.execSQL("DELETE FROM Contacts WHERE id = ?", new String[]{String.valueOf(id)});
                    cursor = db.query("Contacts", null, null, null, null, null, null);
                    if (cursor.moveToFirst()) {
                        showRecord(cursor);
                    }
                    Toast.makeText(SQLiteActivity.this, "删除成功", Toast.LENGTH_SHORT).show();
                }
            }
        });


        closeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (cursor != null) {
                    cursor.close();
                }
                if (db != null) {
                    db.close();
                }
                Toast.makeText(SQLiteActivity.this, "通讯录已关闭", Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void showRecord(Cursor cursor) {
        int nameIndex = cursor.getColumnIndex("name");
        int phoneIndex = cursor.getColumnIndex("phone");
        int addressIndex = cursor.getColumnIndex("address");
        int emailIndex = cursor.getColumnIndex("email");

        String name = (nameIndex != -1) ? cursor.getString(nameIndex) : "";
        String phone = (phoneIndex != -1) ? cursor.getString(phoneIndex) : "";
        String address = (addressIndex != -1) ? cursor.getString(addressIndex) : "";
        String email = (emailIndex != -1) ? cursor.getString(emailIndex) : "";

        nameEdit.setText(name);
        phoneEdit.setText(phone);
        addressEdit.setText(address);
        emailEdit.setText(email);
    }

}    